<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
*/
!defined('IN_MUDDER') && exit('Access Denied');
class GbookModel {
    var $db = null;
    var $tablename = 'gbooks';

    function GbookModel() {
        global $db,$dbpre;

        $this->db = &$db;
        $this->tablename = $dbpre.$this->tablename;
    }

    function getTotal($uid) {
        return $total = $this->db->get_value("SELECT COUNT(*) FROM {$this->tablename} WHERE uid='{$uid}'");
    }

    function & read($uid, $limit) {
        if(is_numeric($limit) && $limit>0) {
            $limit = "0, $limit";
        } elseif(is_array($limit)&&count($limit)==2) {
            $limit = $limit[0].",".$limit[1];
        } else {
            $limit = "0,10";
        }
        $result = array();
        $query = $this->db->query("SELECT * FROM {$this->tablename} WHERE uid='{$uid}' ORDER BY posttime DESC LIMIT $limit");
        while($row = $this->db->fetch_array($query)) {
            $row['content'] = strip_textarea($row['content']);
            $result[] = $row;
        }
        $this->db->free_result($query);
        if(empty($result)) $result = false;
        return $result;
    }

    function save($uid, & $post) {
        $data = array();
        $data['uid'] = $uid;
        $data['gbuid'] = $post['gbuid'];
        $data['gbusername'] = $post['gbusername'];
        $data['content'] = $post['content'];
        $data['posttime'] = $post['posttime'];

        $this->db->insert_new($this->tablename, $data);
        unset($data);
    }

    function delete($gids) {
    }

}
?>